﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

/// <summary>
///一些公共的辅助类
/// </summary>
public class Common
{
    public Common()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    /// <summary>
    /// 获取单位
    /// </summary>
    /// <param name="UNITS">单位字符串</param>
    /// <param name="INDEX">序号</param>
    /// <returns></returns>
    public string GetUnit(string UNITS, string INDEX)
    {
        string[] s = UNITS.Trim().Split('#');
        try
        {
            return s[Convert.ToInt32(INDEX) - 1];
        }
        catch (Exception)
        {
            return string.Empty;
            // throw;
        }
    }
    /// <summary>
    /// 创建转换表
    /// </summary>
    /// <param name="ABSCISSA">横坐标</param>
    /// <returns></returns>
    public DataTable GetTable(string ABSCISSA)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(ABSCISSA);
        dt.Columns.Add("IndicatorsCN");
        dt.Columns.Add("Indicators");
        dt.Columns.Add("IndicatorsValue");
        return dt;
    }
    /// <summary>
    /// 调用存储过程
    /// </summary>
    /// <param name="PRODUCENAME">存储过程名称</param>
    /// <param name="PARAM">参数数组(参数名称、参数类型)</param>
    /// <param name="VALUES">值数组</param>
    /// <returns></returns>
    public DataSet GetList(string PRODUCENAME, string[,] PARAM, string[] VALUES)
    {
        if (PARAM != null)
        {
            /*
            *   Rank为取得数组的秩（维数），可以理解为一个矩阵的行数
            *   GetUpperBound(0) 返回 Array 的第一维的索引上限，GetUpperBound(Rank-1) 返回 Array 的最后一维的上限，也就是列数-1
            */
            int length = PARAM.GetUpperBound(0) + 1;
            MySqlParameter[] param = new MySqlParameter[length];
            for (int i = 0; i < length; i++)
            {
                switch (PARAM[i, 1].ToString().ToLower())
                {
                    case "int":
                        param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.Int32);
                        param[i].Value = Convert.ToInt32(VALUES[i]);
                        break;
                    case "date":
                        param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.Date);
                        param[i].Value = Convert.ToDateTime(VALUES[i]);
                        break;
                    case "datetime":
                        param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.DateTime);
                        param[i].Value = Convert.ToDateTime(VALUES[i]);
                        break;
                    case "varchar":
                        param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.VarChar);
                        param[i].Value = VALUES[i];
                        break;
                    default:
                        param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.String);
                        param[i].Value = VALUES[i];
                        break;
                }

            }
            return MySQLHelper.ExecuteDataset(CommandType.StoredProcedure, PRODUCENAME, param);
        }
        return MySQLHelper.ExecuteDataset(CommandType.StoredProcedure, PRODUCENAME, null);
    }
    /// <summary>
    /// 获取时间格式化字符串,时间相差超过一天就显示天，超过一月就显示月，默认为显示时间
    /// </summary>
    /// <param name="sTime">开始时间</param>
    /// <param name="eTime">结束时间</param>
    /// <returns></returns>
    public string GetTimeFormat(string sTime, string eTime) {
        try
        {
            DateTime sdt = Convert.ToDateTime(sTime);
            DateTime edt = Convert.ToDateTime(eTime);
            TimeSpan ts = edt.Subtract(sdt);
            if (ts.Days > 30)
            {
                return "M月dd日HH时m分";
            }
            if (ts.Days > 1)
            {
                return "dd日HH时m分";
            }
            else {
                return "HH时m分";
            }
        }
        catch (Exception)
        {
            return "HH时m分";
            //throw;
        }
    }
}
